package com.seecen.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.mapper.UserMapper;
import com.seecen.pojo.User;
import com.seecen.service.UserService;
import com.seecen.until.JwtHelper;
import com.seecen.until.MD5Util;
import com.seecen.until.Result;
import com.seecen.until.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author 15487
 * @description 针对表【news_user】的数据库操作Service实现
 * @createDate 2025-03-06 14:54:00
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private JwtHelper jwtHelper;

    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(wrapper);
        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        if (!StringUtils.isEmpty(user.getPassword()) && user.getPassword().equals(loginUser.getPassword())) {
            //登录成功
            //根据用户id生成token
            String token = jwtHelper.createToken(loginUser.getId());
            HashMap map = new HashMap();
            map.put("token", token);
            return Result.ok(map);
        }
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result getUserInfo(String token) {
        //token是否已经过期
        boolean isExpiration = jwtHelper.isExpiration(token);
        if (isExpiration){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        //解析用户id
        Long userId = jwtHelper.getUserId(token);
        //
        User user = userMapper.selectById(userId);
        //防止泄密
        user.setPassword("");

        HashMap map = new HashMap();
        map.put("loginUser",user);
        return Result.ok(map);
    }

    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);

        Long count =userMapper.selectCount(wrapper);
        if (count==0){
            return Result.ok(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    @Override
    public Result register(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        Long count = userMapper.selectCount(wrapper);
        if (count>0){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        user.setPassword(MD5Util.encrypt(user.getPassword()));
        userMapper.insert(user);

        return Result.ok(null);
    }
}




